package com.xunfang.shareaddress;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.MapActivity;
import com.baidu.mapapi.MapController;
import com.baidu.mapapi.MapView;
import com.xunfang.shareaddress.util.PosOverLay;
/**
 * 
 * 显示地图内容
 *
 */
public class ShowMapActivity extends MapActivity {
	//实例化控件
	private BMapManager mBMapMan = null;
	private String ausername;
	private  RadioGroup rg;
	private  RadioButton generalMap,SatelliteMap;
	private  MapView mMapView=null;
	
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.mlayout);
	    //根据组件ID获取组件对象
	    rg=(RadioGroup)findViewById(R.id.rg);
	    generalMap=(RadioButton)findViewById(R.id.generalMap);
	    SatelliteMap=(RadioButton)findViewById(R.id.SatelliteMap);
	    
		mBMapMan = new BMapManager(getApplication());
		mBMapMan.init("7994E9433E9A31FA26B73BA6F7596D43DAF5A32D", null);
		super.initMapActivity(mBMapMan);
		mMapView = (MapView) findViewById(R.id.bmapsView);
		mMapView.setBuiltInZoomControls(true);
		// 设置启用内置的缩放控件
		MapController mMapController = mMapView.getController(); // 得到mMapView的控制权,可以用它控制和驱动平移和缩放
	    //获取传递过来的经纬度
	    Intent intent=getIntent();
	    String keys=intent.getStringExtra("key");
	    String jingdu=intent.getStringExtra("jd");
	    String weidu=intent.getStringExtra("wd");
	    
	    if(keys.equals("look")){
	    ausername=intent.getStringExtra("username");
	    System.out.println("经纬度"+jingdu+","+weidu);	    
	    if(jingdu!=null&&jingdu!=null){
	    	//经纬度
	    List<String> list=new ArrayList<String>();
	    list.add(ausername);
	    list.add(jingdu);
	    list.add(weidu);
		GeoPoint point = new GeoPoint((int) (Double.valueOf(jingdu)* 1E6),
				(int) (Double.valueOf(weidu) * 1E6)); // 用给定的经纬度构造一个GeoPoint，单位是微度 (度 * 1E6)
		mMapController.setCenter(point); // 设置地图中心点
		mMapController.setZoom(12); // 设置地图zoom级别
		if(list.size()>0){
         upMapview(list);//更新地图
		}
	    }
	    }else{
	    //获取共享位置信息
	    List<String>  list=new ArrayList<String>();
	    ausername=intent.getStringExtra("username");  
	    	list.addAll(intent.getStringArrayListExtra("ShareAddress"));
	    if(list.size()>0){
			GeoPoint point = new GeoPoint((int) (Double.valueOf(jingdu)* 1E6),
					(int) (Double.valueOf(weidu) * 1E6)); // 用给定的经纬度构造一个GeoPoint，单位是微度 (度 * 1E6)
			mMapController.setCenter(point); // 设置地图中心点
			mMapController.setZoom(12); // 设置地图zoom级别
	    upMapview(list);//更新地图
	    }
	    }
	
		//给单选按钮组设置点击监听事件
	    rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {		
			
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				switch (checkedId) {
				//选中普通地图
				case R.id.generalMap:
					mMapView.setSatellite(false);
					break;
					//选中卫星地图
				case R.id.SatelliteMap:
					mMapView.setSatellite(true);
					break;
				default:
					break;
				}
			}
		});
	}

	
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	
	protected void onDestroy() {
		if (mBMapMan != null) {
			mBMapMan.destroy();
			mBMapMan = null;
		}
		super.onDestroy();
	}

	
	protected void onPause() {
		if (mBMapMan != null) {
			mBMapMan.stop();
		}
		super.onPause();
	}

	
	protected void onResume() {
		if (mBMapMan != null) {
			mBMapMan.start();
		}
		super.onResume();
	}
	public void upMapview(List<String> list){
		Drawable marker = getResources().getDrawable(R.drawable.pos); 
		//得到需要标在地图上的资源
		mMapView.getOverlays().add(new PosOverLay(marker, this,list)); //添加ItemizedOverlay实例到mMapView	
	}
}
